/*  2023/03/22 by DPH
*   建网过程中使用的勤务帧的头部
*   使用 自举中心勤务帧 来建网
*
*
*/
#include "ns3/assert.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/address-utils.h"
#include "serve-header.h"

namespace ns3{

NS_LOG_COMPONENT_DEFINE ("ServeHeader");
NS_OBJECT_ENSURE_REGISTERED (ServeHeader);


TypeId ServeHeader::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::ServeHeader")
    .SetParent<Header> ()
    .SetGroupName ("Internet-Apps")
    .AddConstructor<ServeHeader> ()
  ;
  return tid;
}

TypeId ServeHeader::GetInstanceTypeId (void) const
{
  return GetTypeId ();
}

void ServeHeader::Print (std::ostream &os) const
{
    os << "(type=" << m_serveType << ")";
}


void
ServeHeader::Serialize (Buffer::Iterator start) const
{
    Buffer::Iterator i = start;
    uint8_t sourAddr = m_sourAddr.Get() % 256;
    uint8_t destAddr = m_destAddr.Get() % 256;
    i.WriteU8(sourAddr);    // 源网络序号
    i.WriteU8(destAddr);    // 目的网络序号
    i.WriteU8(m_nodeState); // 身份状态
    i.WriteU8(m_serveType); // 服务类型
    i.WriteU8(m_sourIdHigh);// 发端ID 高
    i.WriteU8(m_sourIdLow); // 发端ID 低
    i.WriteU8(m_message0);  // 消息体0
    i.WriteU8(m_message1);  // 消息体1
    i.WriteU8(m_message2);  // 消息体2
    i.WriteU8(m_message3);  // 消息体3
    i.WriteU8(m_message4);  // 消息体4
    i.WriteU8(m_message5);  // 消息体5
    i.WriteU8(m_message6);  // 消息体6
    i.WriteU8(m_message7);  // 消息体7
    i.WriteU8(m_message8);  // 消息体8
    i.WriteU8(m_message9);  // 消息体9
    i.WriteU8(m_reserve);   // 保留
    i.WriteHtonU16(m_check);// 校验和
}

uint32_t ServeHeader::Deserialize(Buffer::Iterator start)
{
    //uint32_t len, clen = start.GetSize();

    Buffer::Iterator i = start;

    uint8_t sourAddr = i.ReadU8();
    uint8_t destAddr = i.ReadU8();
    m_sourAddr.Set(sourAddr + 0x0a010100);
    m_destAddr.Set(destAddr + 0x0a010100);
    m_nodeState = i.ReadU8();
    m_serveType = i.ReadU8();
    m_sourIdHigh = i.ReadU8();
    m_sourIdLow = i.ReadU8();
    m_message0 = i.ReadU8();
    m_message1 = i.ReadU8();
    m_message2 = i.ReadU8();
    m_message3 = i.ReadU8();
    m_message4 = i.ReadU8();
    m_message5 = i.ReadU8();
    m_message6 = i.ReadU8();
    m_message7 = i.ReadU8();
    m_message8 = i.ReadU8();
    m_message9 = i.ReadU8();
    m_reserve = i.ReadU8();
    m_check = i.ReadNtohU16();
    
    // 先返回19 没有用
    return 19;

}




// 构造函数
ServeHeader::ServeHeader()
{
    m_sourAddr = Ipv4Address("0.0.0.0");        // 只取最后8位 默认全0 记得修改
    m_destAddr = Ipv4Address("255.255.255.255");// 默认广播
    m_nodeState = ISOLATED_NODE;                // 默认是孤立节点
    m_serveType = REQUCENTER;                   // 默认竞争中心
    m_sourIdHigh = 0;                           // 源ID 默认0x00
    m_sourIdLow = 0;
    m_message0 = 0;
    m_message1 = 0;
    m_message2 = 0;
    m_message3 = 0;
    m_message4 = 0;
    m_message5 = 0;
    m_message6 = 0;
    m_message7 = 0;
    m_message8 = 0;
    m_message9 = 0;

    m_reserve = 0;
    // 校验和暂不实现
    m_check = 0;

}

ServeHeader::~ServeHeader()
{

}

void
ServeHeader::SetType(uint8_t type)
{
    m_serveType = type;
}

uint8_t
ServeHeader::GetType (void) const
{
    return m_serveType;
}

uint8_t
ServeHeader::GetSourIdHigh (void) const
{
    return m_sourIdHigh;
}


uint8_t
ServeHeader::GetSourIdLow (void) const
{
    return m_sourIdLow;
}

void 
ServeHeader::SetIdHigh(uint8_t id){
    m_sourIdHigh = id;
}

void 
ServeHeader::SetIdLow(uint8_t id){
    m_sourIdLow = id;
}

uint32_t 
ServeHeader::GetSerializedSize (void) const
{
  // return m_len;
  // 默认是19字节， 先不改
  return 19;
}



}



